package com.gjintel.sdk.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.gjintel.framework.holidayset.service.IHolidaySetService;

/**
 * Created by yi.wang1 on 2014/12/29.
 */
public class DateUtils {
	private static final Logger LOG = Logger.getLogger(DateUtils.class);

	private DateUtils() {
	}

	public static Date getDate(long time) {
		long days = (time + 8 * 60 * 60) / (60 * 60 * 24 * 1000);
		return new Date(days * 24 * 60 * 60 * 1000);
	}

	public static Date nextDate(Date date) {
		long time = date.getTime() + 60 * 60 * 24 * 1000;
		return new Date(time);
	}

	public static String simpleDateFormat(Date date, String pattern) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}

	/**
	 * 获取月份的第一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getFirstDayFromMonth(Integer year, Integer month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1, 0, 0, 0);
		return calendar.getTime();
	}

	public static Date simpleDateFormat(String dateString, String pattern) {
		if (dateString == null || "".equals(dateString)) {
			return null;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		try {
			return simpleDateFormat.parse(dateString);
		} catch (Exception e) {
			LOG.debug("格式化日期出现了异常！", e);
			return null;
		}
	}

	/**
	 * 获取今天的字符串
	 * 
	 * @return “20151201”
	 */
	public static String getTodayString() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(calendar.getTime());
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			smdate = sdf.parse(sdf.format(smdate));
			bdate = sdf.parse(sdf.format(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	// 日期转化为大小写
	public static String dataToUpper(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH) + 1;
		int day = ca.get(Calendar.DAY_OF_MONTH);
		return numToUpper(year) + "年" + monthToUppder(month) + "月";// +
																	// dayToUppder(day)
																	// + "日";
	}

	/***
	 * <b>function:</b> 将数字转化为大写
	 * 
	 * @createDate 2010-5-27 上午10:28:12
	 * @param num
	 *            数字
	 * @return 转换后的大写数字
	 */
	public static String numToUpper(int num) {
		// String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
		// String u[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
		String u[] = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
		char[] str = String.valueOf(num).toCharArray();
		String rstr = "";
		for (int i = 0; i < str.length; i++) {
			rstr = rstr + u[Integer.parseInt(str[i] + "")];
		}
		return rstr;
	}

	/***
	 * <b>function:</b> 月转化为大写
	 * 
	 * @createDate 2010-5-27 上午10:41:42
	 * @param month
	 *            月份
	 * @return 返回转换后大写月份
	 */
	public static String monthToUppder(int month) {
		if (month < 10) {
			return numToUpper(month);
		} else if (month == 10) {
			return "十";
		} else {
			return "十" + numToUpper(month - 10);
		}
	}

	/***
	 * <b>function:</b> 日转化为大写
	 * 
	 * @createDate 2010-5-27 上午10:43:32
	 * @param day
	 *            日期
	 * @return 转换大写的日期格式
	 */
	public static String dayToUppder(int day) {
		if (day < 20) {
			return monthToUppder(day);
		} else {
			char[] str = String.valueOf(day).toCharArray();
			if (str[1] == '0') {
				return numToUpper(Integer.parseInt(str[0] + "")) + "十";
			} else {
				return numToUpper(Integer.parseInt(str[0] + "")) + "十" + numToUpper(Integer.parseInt(str[1] + ""));
			}
		}
	}

	/**
	 * @description 日期的计算(去除工作日)
	 * @author wwang
	 * @date 2016-8-24
	 * @param ymd
	 *            年月日
	 * @param sTime
	 *            时分秒
	 * @param days
	 *            天数
	 * @return
	 */
	public static String computeDate(Date ymd, String sTime, int days) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cd = Calendar.getInstance();
			cd.setTime(ymd);
			cd.add(Calendar.DATE, days);// 增加天数
			String tempDate = sdf.format(cd.getTime());// 年月日
			tempDate += " " + sTime;// 拼接时分秒
			return tempDate;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 判断是否为双休日
	 */
	public static void isOverDay(String dates) { // 指定时间判断是否是双休日
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d;
		try {
			d = df.parse(dates);
			if (d.getDay() == 0 || d.getDay() == 6) {
				System.out.println("日期:[" + dates + "] 是双休日");
			} else {
				System.out.println("日期:[" + dates + "] 不是双休日");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void isHodliDays() { // 判断是否滴节假日，是否有节假日加班
		// 中国法定节假日期
		String isHoliday = "01-01,01-02,01-03,02-09,02-10,02-11,02-12,02-13,02-14,"
				+ "02-15,04-04,04-05,04-06,04-29,04-30,05-01,06-10,06-11,"
				+ "06-12,09-19,09-20,09-21,10-01,10-02,10-03,10-04,10-05,10-06,10-07";
		// 节假前后加班日期
		String overDay = "01-05,01-06,02-16,02-17,04-07,04-17,04-28,06-08,06-09,09-22,09-19,10-12";
		// 指定的 月份和日期
		String date = "10-12"; // 这里可以自定义
		if (isHoliday.contains(date)) {
			System.out.println("是节假日...");
		}
		if (overDay.contains(date)) {
			System.out.println("节假前后加班日期...");
		}
	}
	
	
	/**
	 * 计算工作日
	 * @param startDate 开始时间
	 * @param day 天数
	 * @return Date
	 */
	public static Date computeWorkDayDate(IHolidaySetService service,Date startDate,int day){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		cd.setTime(startDate);
		cd.add(Calendar.DATE, 1);// 增加天数
		String tempDate = sdf.format(cd.getTime());// 年月日
		tempDate += " 00:00:00" ;// 拼接时分秒
		
		Date workDate=null;
		try {
			workDate = service.getWorkDayByDays(sdf.parse(tempDate), day);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return workDate;
	}
	
	/**
	 * 计算工作日
	 * @param startDate 开始时间
	 * @param day 天数
	 * @return String
	 */
	public static String computeWorkDayDateStr(IHolidaySetService service,Date startDate,int day){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		cd.setTime(startDate);
//		cd.add(Calendar.DATE, -1);// 减1天数
		String tempDate = sdf.format(cd.getTime());// 年月日
		tempDate += " 00:00:00" ;// 拼接时分秒
		
		String workDateStr=null;
		try {
			Date workDate = service.getWorkDayByDays(sdf.parse(tempDate), day);
			sdf = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
			workDateStr=sdf.format(workDate)+" 23:59:59";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return workDateStr;
	}

	public static void main(String[] args) throws Exception {
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// Date d1 = sdf.parse("2012-09-15 10:10:10");
		// Date d2 = sdf.parse("2017-09-15 00:00:00");
		// System.out.println(daysBetween(d1, d2));

		// System.out.println(DateUtils.dataToUpper(new Date()));
		//isOverDay("2016-11-19");
		//isHodliDays();
		//DateUtils.computeWorkDayDateStr(new Date(), 2);
	}
}
